package org.sanm.config;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

import java.util.Arrays;

@Configuration
@OpenAPIDefinition(security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
@ConditionalOnClass({OpenAPI.class})
public class SwaggerConfig {

    @Value("${server.servlet.context-path}")
    String contextPath;


    @Bean
    public OpenAPI openAPI() {



        return new OpenAPI()
                .info(new Info().title("智农建材供应链")
                        .description("Test Swagger debugging")
                        .version("v1"))
                .servers(Arrays.asList(
                    new Server().url(contextPath).description("服务器")

                ))
                .components(new Components()
                        .addSecuritySchemes(HttpHeaders.AUTHORIZATION,
                                new SecurityScheme()
                                        .name(HttpHeaders.AUTHORIZATION)
                                        .type(SecurityScheme.Type.APIKEY)
                                        .in(SecurityScheme.In.HEADER)
                                        .scheme("Bearer")
                                        .bearerFormat("JWT")
                        )
                );


    }


}
